package m.jclecai.com.fragment;


import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import m.jclecai.com.R;
import m.jclecai.com.activity.LotteryProtocolActivity;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.ChannelUtil;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;


/**
 * 手机号注册
 */
public class RegisterTowFragment extends BaseFragment {

    //推荐人
    @BindView(R.id.et_name)
    EditText et_name;
    //手机号码
    @BindView(R.id.et_receive_prize)
    EditText et_receive_prize;
    //图形验证码
    @BindView(R.id.et_graph_code)
    EditText et_graph_code;
    //接受验证码
    @BindView(R.id.et_auth_code)
    EditText et_auth_code;
    //发送验证码
    @BindView(R.id.tv_send_proof)
    TextView tv_send_proof;
    //提交
    @BindView(R.id.text_bottom)
    TextView text_bottom;

    @BindView(R.id.iv_clear_phone)
    ImageView iv_clear_phone;
    //加载gif图片的
    @BindView(R.id.iv_gf)
    ImageView iv_gf;

    @BindView(R.id.et_user)
    EditText et_user;
    @BindView(R.id.et_password)
    EditText et_password;
    //注册基本信息
    @BindView(R.id.ll_next)
    LinearLayout ll_next;
    //注册密码
    @BindView(R.id.ll_regist)
    LinearLayout ll_regist;

    @BindView(R.id.tv_reg)
    TextView tv_reg;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_register_item2;
    }

    @Override
    public void initView() {
        super.initView();
        tv_reg.setVisibility(View.GONE);
        ll_next.setVisibility(View.VISIBLE);
        ll_regist.setVisibility(View.GONE);
        //获取图形验证码
        getImgCode();
    }


    //监听文本
    @OnTextChanged(R.id.et_receive_prize)
    void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(s.toString())) {
            iv_clear_phone.setVisibility(View.GONE);
        } else {
            iv_clear_phone.setVisibility(View.VISIBLE);
        }
    }

    //清空文本
    @OnClick(R.id.iv_clear_phone)
    void clearTxt() {
        et_receive_prize.setText(null);
    }

    //图形验证码
    @OnClick(R.id.iv_gf)
    void getImgCode() {
        Glide.with(getActivity()).load(Constant_Url.REGISTER_RAND_URL)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                .skipMemoryCache(true).into(iv_gf);//跳过内存缓存
    }

    /**
     * 提交
     */
    @OnClick(R.id.text_bottom)
    void submitResult() {
        String et_name_rg = et_name.getText().toString().trim();
        String et_receive_prize_rg = et_receive_prize.getText().toString().trim();
        String et_graph_code_rg = et_graph_code.getText().toString().trim();//请输入图形验证码
        String et_auth_code_rg = et_auth_code.getText().toString().trim();

        if (TextUtils.isEmpty(et_name_rg)) {
            MyToast.showToast(getActivity(), "请输入推荐人用户名或邀请码");
            return;
        }
        if (TextUtils.isEmpty(et_receive_prize_rg) || !BaseUtils.isMobileNO(et_receive_prize_rg)) {
            MyToast.showToast(getActivity(), "请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(et_graph_code_rg)) {
            MyToast.showToast(getActivity(), "请输入图形验证码");
            return;
        }
        if (TextUtils.isEmpty(et_auth_code_rg)) {
            MyToast.showToast(getActivity(), "请输入手机验证码");
            return;
        }
        register(et_name_rg, et_receive_prize_rg, et_graph_code_rg, et_auth_code_rg);
    }

    //注册-验证用户输入信息合法性(指的是是否存在以及格式)
    private void register(String et_name_rg, String et_receive_prize_rg, String et_graph_code_rg, String et_auth_code_rg) {
        final HashMap<String, String> pair = new HashMap<>();
        pair.put("vphone", et_auth_code_rg);//短信验证码
        pair.put("sessionid", et_graph_code_rg);//图形验证码
        pair.put("phone", et_receive_prize_rg);//手机号码
        pair.put("parentName", et_name_rg);//推荐人姓名
        pair.put("appstore", ChannelUtil.getChannel());//推荐人姓名
        showDialog();
        HttpManager.postAsyn(Constant_Url.CHECK_PHONE_RANDNUM_URL, new HttpManager.StringCallback() {

            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(getActivity(), getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                if (!TextUtils.isEmpty(response)) {
//                    ProofBean
                    ProofBean proofBean = FastOk.get(response, ProofBean.class);
                    if (proofBean != null) {
                        MyToast.showToast(getActivity(), proofBean.getMessage());
                        if (proofBean.getStatus().equals("0")) {
                            //基本信息隐藏 显示注册
                            ll_next.setVisibility(View.GONE);
                            ll_regist.setVisibility(View.VISIBLE);
                            //用户名
                            et_user.setText(proofBean.getUid());
                            //注册按钮
                            tv_reg.setVisibility(View.VISIBLE);
                            //下一步按钮
                            text_bottom.setVisibility(View.GONE);
                        }
                    } else {
                        MyToast.showToast(getActivity(), getString(R.string.error_network));
                    }
                }
            }
        }, pair);
    }

    //注册
    @OnClick(R.id.tv_reg)
    void register() {
        String userName = et_user.getText().toString();
        String password = et_password.getText().toString();
        if (TextUtils.isEmpty(userName)) {
            MyToast.showToast(getActivity(), "用户名不能为空哦!");
            return;
        }
        if (userName.length() < 4 || userName.length() > 16) {
            MyToast.showToast(getActivity(), "用户名长度为4-16位字符!");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            MyToast.showToast(getActivity(), "密码不能为空哦!");
            return;
        }
        if (password.length() < 6 || password.length() > 20) {
            MyToast.showToast(getActivity(), "密码长度为6-20位字符!");
            return;
        }
        final HashMap<String, String> pair = new HashMap<>();
        pair.put("user", userName);//用户姓名
//        pair.put("sessionid", et_graph_code_rg);//图形验证码
        pair.put("phone", et_receive_prize.getText().toString());//手机号码
        pair.put("parentName", et_name.getText().toString());//推荐人姓名
        pair.put("appstore ", ChannelUtil.getChannel());//渠道号码
        pair.put("password", password);//登录密码
        pair.put("flag", "1");//
        showDialog();
        HttpManager.postAsyn(Constant_Url.NEWLY_REGISTER_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(getActivity(), getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                if (!TextUtils.isEmpty(response)) {
                    ProofBean proofBean = FastOk.get(response, ProofBean.class);
                    if (proofBean == null) {
                        MyToast.showToast(getActivity(), getString(R.string.error_network));
                    } else {
                        MyToast.showToast(getActivity(), proofBean.getMessage());
                        if ("0".equals(proofBean.getStatus()))
                            logIn(pair.get("user"), pair.get("password"), new Callback() {
                                @Override
                                public void callbackSuccess() {
                                    super.callbackSuccess();
                                    getActivity().setResult(Activity.RESULT_OK);
                                    getActivity().finish();
                                }

                                @Override
                                public void callbackField() {
                                    super.callbackField();
                                    MyToast.showToast(getActivity(), "登录失败请尝试手动登录！");
                                }
                            });
                    }
                }
            }
        }, pair);
    }

    //发送验证码
    @OnClick(R.id.tv_send_proof)
    void sendProof() {
        final String phone = et_receive_prize.getText().toString().trim();
        if (TextUtils.isEmpty(phone) || !BaseUtils.isMobileNO(phone)) {
            MyToast.showToast(getActivity(), "请输入正确的手机号码");
            return;
        }

        HashMap<String, String> pair = new HashMap<>();
        pair.put("phone", phone);
        HttpManager.postAsyn(Constant_Url.PROOF_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyToast.showToast(getActivity(), "验证码发送失败");
            }

            @Override
            public void onResponse(String response) {
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if (proofBean != null){
                    if ("0".equals(proofBean.getStatus())) {
                        BaseUtils.countDowm(getActivity(), 60, tv_send_proof);
                    }
                    MyToast.showToast(getActivity(), proofBean.getMessage());
                }
                else
                    MyToast.showToast(getActivity(), getString(R.string.error_network));

            }
        }, pair);
    }

    @OnClick(R.id.tv_pact)
    void protocol() {
        startActivity(LotteryProtocolActivity.getIntent(getActivity()));
    }
}
